﻿using Games.ChallengeMemory;
using Games.Common;
using Games.Data;
using Xamarin.Forms;
using static Games.MainPage;

namespace Games
{
    public partial class App : Application
    {
        private IMediaHandler media;
        private IDataBase database;
        private const string Title = "秋夏游戏室 V2.0";
        internal static Settings Settings { get; private set; }
        public App()
        {
            InitializeComponent();
            //var file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "data.db3");
            //File.Delete(file);
            DependencyService.Register<GamePageManager>();
            DependencyService.Register<DataBase>();
            this.media = DependencyService.Get<IMediaHandler>();
            this.database = DependencyService.Get<IDataBase>();
            this.database.CreateTable<Settings>();
            Settings = this.database.GetFirstOrDefaultAsync<Settings>(null).Result;
            if(Settings == null)
            {
                Settings = new Settings { MusicIsOn = true };
                this.database.SaveAsync(Settings);
            }
            var model = new SettingsModel { Title = Title, Music = Settings.MusicIsOn ? "开" : "关" };
            MainPage = new NavigationPage(new MainPage(model));

            Utils.Log(null);
        }

        protected override void OnStart()
        {
            if(Settings.MusicIsOn)
            {
                this.media.Play();
            }
        }

        protected override void OnSleep()
        {
            TimeCounter.Pause();
            if(Settings.MusicIsOn)
            {
                this.media.Pause();
            }
        }

        protected override void OnResume()
        {
            if(Settings.MusicIsOn)
            {
                this.media.Resume();
            }
            TimeCounter.Continue();
        }
    }
}
